package aip.assignment2.view.customer;

import aip.assignment2.persistence.entity.Product;
import aip.assignment2.service.Application;
import aip.assignment2.service.ShoppingCart;
import aip.assignment2.service.entity.ProductInCart;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;
import javax.annotation.PreDestroy;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

/**
 * This managed bean contains all UI data and functions that live through the session scope of the customer.
 */
@ManagedBean(name = "cart")
@SessionScoped
public class Cart implements Serializable
{
	@EJB
	private ShoppingCart m_ShoppingCart;
	@EJB
	private Application m_Application;
	// <editor-fold desc="Property - inputProductQuantity - Get" defaultstate="collapsed">
	/**
	 * the input product quantity
	 */
	private int inputProductQuantity;
	/**
	 * the input product quantity
	 * @return the inputProductQuantity
	 */
	public int getInputProductQuantity()
	{
		inputProductQuantity = 1;
		return inputProductQuantity;
	}
	/**
	 * the input product quantity
	 * @param inputProductQuantity the inputProductQuantity to set
	 */
	public void setInputProductQuantity(int inputProductQuantity)
	{
		this.inputProductQuantity = inputProductQuantity;
	}
	// </editor-fold>
	/**
	 * This method adds products to the shopping cart.
	 * @param product the product to be added
	 * @param quantity the quantity of the product
	 */
	public void addProduct(Product product)
	{
		if (m_ShoppingCart.add(product, inputProductQuantity))
		{
			m_Application.addMessage(FacesMessage.SEVERITY_INFO, String.format("%d \"%s\" are added to the shopping cart.", inputProductQuantity, product.getName()));
		}
		else
		{
			m_Application.addMessage(FacesMessage.SEVERITY_ERROR, "Unable to add products to the shopping cart.");
		}
	}
	/**
	 * This method gets the current products in the shopping cart.
	 * @return the list of ProductInCart
	 */
	public List<ProductInCart> getProducts()
	{
		return m_ShoppingCart.getProductsInCart();
	}
	/**
	 * This method updates the existing product's quantity.
	 * @param product the product to be updated
	 */
	public void updateProductInCart(ProductInCart productInCart)
	{
		if (m_ShoppingCart.update(productInCart.getProduct(), productInCart.getQuantity()))
		{
			m_Application.addMessage(FacesMessage.SEVERITY_INFO, String.format("The quantity of the product \"%s\" is updated.", productInCart.getProduct().getName()));
		}
		else
		{
			m_Application.addMessage(FacesMessage.SEVERITY_ERROR, "Unable to update product quantity.");
		}
	}
	/**
	 * This method removes an existing product in the shopping cart.
	 * @param productInCart the product to be removed
	 */
	public void removeProductInCart(ProductInCart productInCart)
	{
		if (m_ShoppingCart.remove(productInCart.getProduct()))
		{
			m_Application.addMessage(FacesMessage.SEVERITY_INFO, String.format("The product \"%s\" is removed.", productInCart.getProduct().getName()));
		}
		else
		{
			m_Application.addMessage(FacesMessage.SEVERITY_ERROR, "Unable to remove the product.");
		}
	}
	/**
	 * This method clears the shopping cart.
	 */
	public void clear()
	{
		m_ShoppingCart.clear();
		m_Application.addMessage(FacesMessage.SEVERITY_INFO, String.format("The shopping cart is cleared."));
	}
	/**
	 * This method gets the total price of the shopping cart.
	 * @return the total price
	 */
	public BigDecimal getTotalPrice()
	{
		return m_ShoppingCart.getTotalPrice();
	}
}
